function logItems(bookmarkItems) {
  let localMarks = [];
  for(const bookmarkItem of bookmarkItems) {
    if(bookmarkItem.type === "folder") {
      // folder
      localMarks = localMarks.concat(logItems(bookmarkItem.children));
    } else {
      // bookmark
      localMarks.push({
        bookmark_id: bookmarkItem.id,
        title: bookmarkItem.title,
        index: bookmarkItem.index,
        data_added: bookmarkItem.dateAdded,
        type: bookmarkItem.type,
        url: bookmarkItem.url,
        parent_id: bookmarkItem.parentId
      });
    }
  }
  return localMarks;
}

function logTree(bookmarkItems) {
  marks = logItems(bookmarkItems);
  $.ajax({
    url: "http://127.0.0.1:8080/api/bookmark",
    type: "PUT",
    contentType: "application/json",
    data: JSON.stringify(marks),
    success: function(result) {
      console.log(result);
    },
    error: function(a, b, c) {
      console.log(a);
      console.log(b);
      console.log(c);
    }
  })
}

function onRejected(error) {
  console.log(`An error: ${error}`);
}

let gettingTree = browser.bookmarks.getTree();

gettingTree.then(logTree, onRejected);